<template>
	<!-- 新增发票抬头 -->
	<view class="addArchives-main">
		<uni-forms ref="baseForm" :modelValue="baseFormData">
			<uni-forms-item label="抬头类型" required label-width="160rpx">
				<uni-data-select v-model="baseFormData.type" :localdata="feedbackType"></uni-data-select>
			</uni-forms-item>
			<uni-forms-item label="姓名/单位" required label-width="160rpx">
				<uni-easyinput v-model="baseFormData.name" placeholder="请输入姓名/单位" />
			</uni-forms-item>
			<view v-if="baseFormData.type == '1'">
				<uni-forms-item label="单位税号" required label-width="160rpx">
					<uni-easyinput v-model="baseFormData.company_no" placeholder="请输入单位税号" />
				</uni-forms-item>
				<uni-forms-item label="单位地址" required label-width="160rpx">
					<uni-easyinput v-model="baseFormData.company_address" placeholder="请输入单位地址" />
				</uni-forms-item>
				<uni-forms-item label="单位电话" required label-width="160rpx">
					<uni-easyinput v-model="baseFormData.company_tel" placeholder="请输入单位电话" />
				</uni-forms-item>
				<uni-forms-item label="开户银行" required label-width="160rpx">
					<uni-easyinput v-model="baseFormData.company_bank" placeholder="请输入开户银行" />
				</uni-forms-item>
				<uni-forms-item label="银行账号" required label-width="160rpx">
					<uni-easyinput v-model="baseFormData.company_bank_no" placeholder="请输入银行账号" />
				</uni-forms-item>
			</view>

			<uni-forms-item label="联系邮箱" required label-width="160rpx">
				<uni-easyinput v-model="baseFormData.email" placeholder="请输入联系邮箱" />
			</uni-forms-item>
			<uni-forms-item label="是否默认" required label-width="160rpx">
				<u-switch activeColor="#f56c6c" v-model="baseFormData.default" style="margin-top: 8rpx" activeValue="1" inactiveValue="0"></u-switch>
			</uni-forms-item>
		</uni-forms>

		<view class="bankCard-btn">
			<view class="btn" @click="getuserinvoiceSave">
				{{ idNum ? '提交编辑' : '确认添加' }}
			</view>
		</view>
	</view>
</template>

<script>
import { userinvoiceSave, userInvoiceInfo } from '../../api/home/index.js';
import { validatePhoneNumber,validateEmail,validateBankAccount,validateTaxID } from '../../utils/tools.js';
export default {
	data() {
		return {
			baseFormData: {
				type: '',
				name: '',
				company_no: '',
				company_address: '',
				company_tel: '',
				company_bank: '',
				company_bank_no: '',
				email: '',
				default: '0'
			},
			feedbackType: [
				{
					value: '0',
					text: '个人'
				},
				{
					value: '1',
					text: '企业'
				}
			],
			idNum:false
		};
	},
	onLoad(option) {
		const { id } = option;
		if (id) {
			this.idNum = true;
			this.getuserInvoiceInfo(id);
			uni.setNavigationBarTitle({
				title: '编辑发票抬头'
			});
		}
	},
	methods: {
		// 添加抬头
		async getuserinvoiceSave() {
			if (!this.baseFormData.name) {
				return uni.showToast({
					title: '姓名/单位不能为空',
					icon:'none'
				});
			}

			if (this.baseFormData.type == 1) {
				if (!this.baseFormData.company_no) {
					return uni.showToast({
						title: '单位税号不能为空',
						icon:'none'
					});
				}
				
				if(!validateTaxID(this.baseFormData.company_no)){
					return uni.showToast({
						title: '单位税号格式错误',
						icon:'none'
					});
				}
				
				if (!this.baseFormData.company_address) {
					return uni.showToast({
						title: '单位地址不能为空',
						icon:'none'
					});
				}
				
				if (!this.baseFormData.company_tel) {
					return uni.showToast({
						title: '单位电话不能为空',
						icon:'none'
					});
				}
				
				if(!validatePhoneNumber(this.baseFormData.company_tel)){
					return uni.showToast({
						title: '电话格式错误',
						icon:'none'
					});
				}
				
				if (!this.baseFormData.company_bank) {
					return uni.showToast({
						title: '开户银行不能为空',
						icon:'none'
					});
				}
				
				if (!this.baseFormData.company_bank_no) {
					return uni.showToast({
						title: '银行账号不能为空',
						icon:'none'
					});
				}
				
				if(!validateBankAccount(this.baseFormData.company_bank_no)){
					return uni.showToast({
						title: '银行账号格式错误',
						icon:'none'
					});
				}
			}
			
			if (!this.baseFormData.email) {
				return uni.showToast({
					title: '联系邮箱不能为空',
					icon:'none'
				});
			}
			if(!validateEmail(this.baseFormData.email)){
				return uni.showToast({
					title: '邮箱格式错误',
					icon:'none'
				});
			}

			try {
				const resData = await userinvoiceSave(this.baseFormData);
				uni.showToast({
					title: '添加成功',
					icon: 'none'
				});
				setTimeout(() => {
					uni.navigateBack(-1);
				}, 500);
			} catch (e) {
				throw Error(e);
			}
		},

		// 获取抬头信息
		async getuserInvoiceInfo(id) {
			try {
				const resData = await userInvoiceInfo({
					id
				});
				this.baseFormData = resData.data;
			} catch (e) {
				throw Error(e);
			}
		}
	}
};
</script>

<style>
page {
	background-color: #fff;
}
</style>

<style scoped lang="scss">
.addArchives-main {
	box-sizing: border-box;
	padding: 20rpx;
	.bankCard-btn {
		box-sizing: border-box;
		padding: 20rpx;
		background-color: #fff;
		position: sticky;
		bottom: 0px;
		left: 0px;
		width: 100%;
		.btn {
			background-color: #e43927;
			color: #fff;
			height: 80rpx;
			border-radius: 8px;
			line-height: 80rpx;
			text-align: center;
		}
	}
}
</style>
